/*
 * Copyright (C) 2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

/*
 * Prototypes
 */
#if PROTO
static void
fp_proj_fp(f_t *u, f_t *v, eint32_t m);

static void
dp_proj_dp(d_t *u, d_t *v, eint32_t m);

static void
vo_proj_vo(vo_t *u, vo_t *v, eint32_t m);
#endif

/*
 * Implementation
 */
#if CODE
void
evector_proj(evector_t *v, etype_t t)
{
    eint32_t	 l;
    evector_t	*r;
    v_enter();

    switch (t) {
	case t_int8:	case t_uint8:
	case t_int16:	case t_uint16:
	case t_int32:	case t_uint32:
	case t_int64:	case t_uint64:
	    /* don't coerce t_uint64 to a dynamically typed vector */
	    thread_self->obj = v;
	    return;
	case t_float32:	case t_float64:
	case t_void:
	    break;
	default:
	    eraise_exception(except_not_a_number);
    }

    v_check(1);
    l = v->length;
    r = create(v, t);
    switch (t) {
	case t_float32:
	    fp_proj_fp(r->v.obj, v->v.obj, l);
	    break;
	case t_float64:
	    dp_proj_dp(r->v.obj, v->v.obj, l);
	    break;
	default:
	    vo_proj_vo(r->v.obj, v->v.obj, l);
	    break;
    }
    v_leave();
}

static void
fp_proj_fp(f_t *u, f_t *v, eint32_t m)
{
    for (; m; m--, u++, v++) {
#if !HAVE_INSINFF
#  define isinff(f)			isinf((double)(f))
#endif
	if (likely(!isinff(*v) || *v > 0.0))
	    *u = *v;
	else
	    *u = INFINITY;
    }
}

static void
dp_proj_dp(d_t *u, d_t *v, eint32_t m)
{
    for (; m; m--, u++, v++) {
	if (likely(!isinf(*v) || *v > 0.0))
	    *u = *v;
	else
	    *u = INFINITY;
    }
}

static void
vo_proj_vo(vo_t *u, vo_t *v, eint32_t m)
{
    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		set_i(0);
		break;
	    case t_int:
		set_i(get_i(v));
		break;
	    case t_float:
		set_d(ed_proj_d(get_d(v)));
		break;
	    case t_mpz:
		set_z(get_z(v));
		break;
	    case t_mpq:
		set_q(get_q(v));
		break;
	    case t_mpr:
		er_proj_r(thr_rr, get_r(v));
		set_r(thr_rr);
		break;
	    case t_cdd:
		set_dd(edd_proj_dd(get_dd(v)));
		break;
	    case t_cqq:
		set_qq(get_qq(v));
		break;
	    case t_mpc:
		ecc_proj_cc(thr_c, get_cc(v));
		set_cc(thr_c);
		break;
	    default:
		eraise_exception(except_not_a_number);
	}
    }
}
#endif
